<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>构造函数</title>
</head>
<body>
<script type="text/javascript">
    //构造函数创建对象：里面的方法，每次实例化都会重新定义一个function,浪费内存，
    //因此将函数提出来
    function f1(){console.log("你好我叫"+this.name);}
    function Person(id, name){
        this.id = id;
        this.name = name;
        this.say = f1;
    }
    var p1 = new Person(1,"张三");
    var p2 = new Person(2,"李四");
    console.log(p1);
    console.log(p2);
    //但若是函数非常多的时候
    var obj = {
        "fn1":function(){
            console.log(this.name);
        },
        "fn2":function(){
            console.log(this.name);
        }
    }
    function object(id,name){
        this.id = id;
        this.name = name;
        this.say = obj.fn1;
    }
</script>
</body>
</html>